package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.packageSelling;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.PackageSellingStatisticBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.packageSelling.PackageSellingBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.ExportPackageSellingExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.PdtInfExcelEn;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoxu
 * @since 2024-04-23
 */
@CrossOrigin()
@RestController
@RequestMapping(value = "/packageSelling")
public class PackageSellingController {
    @Autowired
    private PackageSellingBu packageSellingBu;
//查询所有打包销售数据、
    @GetMapping(value = "/getAllPackageSelling")
//    Integer page,Integer pageSize,String startTime,String endTime,String pdtId,String coId,String plantId
    public SzResult selectAll(@RequestParam Integer page , @RequestParam Integer pageSize, @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam(required = false) String pdtId, @RequestParam(required = false) String coId, HttpServletRequest request){
        String plantId = request.getHeader("Plant-Id");
        return packageSellingBu.getAllPackageSelling(page,pageSize,startTime,endTime,pdtId,coId,plantId);
    }
//    新增打包销售数据
    @PostMapping(value = "/addPackageSelling")
    @PermissionCheck("addPackageSelling")
    public SzResult addPackageSelling(@RequestBody JSONObject jsonObject,
                                      HttpServletRequest request) throws Exception {
//      String coId, String pdtId, String startTime, String endTime, BigDecimal price,Integer num,Integer sellType,String desc,String dataFrom,String token
        List coIds = jsonObject.getJSONArray("coIds");
        List pdtIds = jsonObject.getJSONArray("pdtIds");
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        BigDecimal price = jsonObject.getBigDecimal("price");
        BigDecimal num = jsonObject.getBigDecimal("num");
        Integer sellType = jsonObject.getInteger("sellType");
        String desc = jsonObject.getString("desc");
        String dataFrom = request.getHeader("Plant-Id");
        String token = request.getHeader("Authorization");
        return packageSellingBu.addPackageSelling(coIds,pdtIds,startTime,endTime,price,num,sellType,desc,dataFrom,token);
    }
//    修改打包销售数据
@PostMapping(value = "/updatePackageSelling")
@PermissionCheck("updatePackageSelling")
public SzResult updatePackageSelling(@RequestBody JSONObject jsonObject,
                                  HttpServletRequest request) throws Exception {
//      String coId, String pdtId, String startTime, String endTime, BigDecimal price,Integer num,Integer sellType,String desc,String dataFrom,String token
    String id = jsonObject.getString("id");
    String coId = jsonObject.getString("coId");
    String pdtId = jsonObject.getString("pdtId");
    String startTime = jsonObject.getString("startTime");
    String endTime = jsonObject.getString("endTime");
    BigDecimal price = jsonObject.getBigDecimal("price");
    BigDecimal num = jsonObject.getBigDecimal("num");
    Integer sellType = jsonObject.getInteger("sellType");
    String desc = jsonObject.getString("desc");
    String dataFrom = request.getHeader("Plant-Id");
    String token = request.getHeader("Authorization");
    return packageSellingBu.updatePackageSelling(id,coId,pdtId,startTime,endTime,price,num,sellType,desc,dataFrom,token);
}
    //       删除打包销售数据
//    public SzResult deletePackageSelling(String id, String dataFrom, String token) throws Exception {
    @PostMapping(value = "/deletePackageSelling")
//    @PermissionCheck("deletePackageSelling")
    public SzResult deletePackageSelling(@RequestBody JSONObject jsonObject,
                                        HttpServletRequest request) throws Exception {
        String id = jsonObject.getString("id");
        String dataFrom = request.getHeader("Plant-Id");
        String token = request.getHeader("Authorization");
        return packageSellingBu.deletePackageSelling(id, dataFrom, token);
    }
//导出打包销售数据excel
    /**
     * 导出excel
     *
     * @param jsonParam 传入的参数 filename 文件名  fields 实体类  ids 数据  isHeader 是否有表头
     * @param response  响应
     * @param request   请求
     * @return void 无返回值 IO流
     * @throws IOException          IO异常
     * @throws ExecutionException   异常
     * @throws InterruptedException 异常
     */
    @PostMapping(value = "/exportPackageSelling")
    public void exportPackageSelling(@RequestBody JSONObject jsonParam, HttpServletResponse response, HttpServletRequest request) throws Exception {
        //      String coId, String pdtId, String startTime, String endTime, BigDecimal price,Integer num,Integer sellType,String desc,String dataFrom,String token
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        List<Map<String, Object>> fields = jsonParam.getObject("fields", List.class);
        String filename = jsonParam.getString("filename");
        List<Map<String, Object>> ids = jsonParam.getObject("ids", List.class);
        boolean isHeader = jsonParam.getBoolean("isHeader");
        String mode = jsonParam.getString("mode");
        Boolean original = jsonParam.getBoolean("original");
        Map<String, String> pager = jsonParam.getObject("pager", Map.class);
        String sheetName = jsonParam.getString("sheetName");
        Map<String, String> form = jsonParam.getObject("form", Map.class);
        Future<List<ExportPackageSellingExcelEn>> listFuture = packageSellingBu.exportExcel(fields, filename, ids, isHeader, mode, original, pager, sheetName, form, plantId);
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        List<ExportPackageSellingExcelEn> exportPackageSellingExcelEn = listFuture.get();
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ExportPackageSellingExcelEn.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("打包销售")
                .doWrite(exportPackageSellingExcelEn);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }
    @PostMapping(value = "/salesStatisticExcel")
    public void salesStatisticExcel(@RequestBody PackageSellingStatisticBo packageSellingStatisticBo, HttpServletResponse response) throws Exception {
        packageSellingBu.salesStatisticExcel(packageSellingStatisticBo.getClientIds(),packageSellingStatisticBo.getProductIds(),packageSellingStatisticBo.getStartTime(),packageSellingStatisticBo.getEndTime(),response);
    }
//    打包价执行
}



